﻿using System;
using System.Collections;
using System.Threading;
using System.Web.Mvc;
using OpenFlashChart;
using Peak.ApplicationServices.Services;
using Peak.Core;
using Peak.Core.DataInterfaces;
using MvcContrib;
using Peak.Web.Controllers.Models;
using Peak.Web.Controllers.Services;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Web.NHibernate;

namespace Peak.Web.Controllers
{
  public class ProjectController : BaseController
  {
    private readonly IProjectService _projectService;
    
    public ProjectController(ISessionService sessionService, IProjectService projectService)
      : base(sessionService)
    {
      _projectService = projectService;           
    }

    public ActionResult Index()
    {
      if(!_sessionService.IsProjectSelected)
          return this.RedirectToAction<ProjectsController>(f => f.Index());      

      return View(new ProjectModel(_sessionService.Project,_sessionService.Project == _sessionService.User.Profile.DefaultProject));
    }

    public ActionResult New()
    {
      return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [Transaction]
    public ActionResult New(ProjectModel project)
    {
      if (!project.IsValid())
      {
        return View(project);
      } 
      Project dbProject = new Project();
      project.Update(dbProject);
      dbProject.Created = DateTime.Now;
      dbProject.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
      dbProject.Users.Add(_sessionService.User);
      dbProject = _projectService.Update(dbProject);
      if (project.Default)
      {
        SetDefaultProject(dbProject);
      }
      
      return this.RedirectToAction<ProjectsController>(f=>f.SelectProject(dbProject.Id));
    }

    private void SetDefaultProject(Project project)
    {
      _sessionService.User.Profile.DefaultProject = project;      
    }

    public ActionResult Edit(int projectId)
    {
      Project project = _projectService.Get(projectId);
      return View(new ProjectModel(project, project == _sessionService.User.Profile.DefaultProject));
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [Transaction]
    public ActionResult Edit(ProjectModel project)
    {
      if(!project.IsValid())
      {
        return View(project);
      }
      Project dbProject = _projectService.Get(project.ProjectId);
      project.Update(dbProject);
      if(project.Default)
      {
        SetDefaultProject(dbProject);
      }
      else
      {
        SetDefaultProject(null);        
      }

      return Back();
    } 

    public ActionResult Burndown()
    {
      OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
      chart.Title = new Title("Bar Chart");
      chart.Bgcolor = "#E7E9DC";

      Candle candle = new OpenFlashChart.Candle();
      Random random = new Random();
      candle.Colour = "#4D5C63";

      candle.FillAlpha = 0.4;
      candle.Text = "Test";

      candle.FontSize = 10;
      ArrayList values = new ArrayList();
      for (int i = 0; i < 12; i++)
      {
        int i1 = random.Next(-20, 30);
        int i2 = random.Next(-15, 20);
        values.Add(new CandleValue(i1, i1, i2, i2));
      }
      candle.Values = values;
      chart.AddElement(candle);
      XAxis xaxis = new XAxis();
      // xaxis.Labels = new AxisLabel("text","#ef0",10,"vertical");
      xaxis.Steps = 1;
      xaxis.Offset = true;
      //xaxis.SetRange(-2, 15);
      chart.X_Axis = xaxis;
      YAxis yaxis = new YAxis();
      yaxis.Steps = 4;
      yaxis.SetRange(0, 20);
      chart.Y_Axis = yaxis;

      chart.Y_Axis.SetRange(-24, 24, 3);
      candle.Tooltip = "Burndown";
      return new ChartDataResult(chart);      
    }
  }
}
